package com.rs.utils;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * 线程池工具类
 */
public class ThreadPoolUtil {

    private volatile static ExecutorService executorService = null;

    /**
     * 获取单例线程池
     *
     * @return 线程池
     */
    public static ExecutorService getExecutorService() {
        if (executorService == null) {
            synchronized (ThreadPoolUtil.class) {
                if (executorService == null) {
                    executorService = new ThreadPoolExecutor(
                            Runtime.getRuntime().availableProcessors() << 2,
                            Runtime.getRuntime().availableProcessors() << 2,
                            60,
                            TimeUnit.SECONDS,
                            new LinkedBlockingQueue<>(1000),
                            new ThreadPoolExecutor.CallerRunsPolicy()
                    );
                }
            }
        }
        return executorService;
    }
}
